<?php
class AppCourseAction extends Action {
	/**
	 * 课程列表
	 * 入参,排序字段,app首页类型1234567
	 * get
	 **/
	public function courseList(){
		$course = M('Course');
		$course_image = M('Course_image');
		if(!empty($_GET['search'])){
			$where['s_course.course_name'] = array('like',"%{$_GET['search']}%");
		}
		$user_long=$_GET['longt'];//经度$_GET['longt']
		$user_lat=$_GET['lat'];//纬度$_GET['lat']
		if(!empty($user_lat) && !empty($user_long)){
			$distance_id=$this->updateUserDistance($user_long,$user_lat);
		}
		$renqi=$_GET['renqi'];
		if(!empty($distance_id)){
			if(empty($renqi)){
				$order = 'temp_distance asc,create_time desc';
			}else {
				$order = 'counts desc,create_time desc';
			}
			if(empty($_GET['id'])){//,ifnull(uc.collection_count,0)+ifnull(ccomment.comment_count,0) as counts,
				$courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image,ud.temp_distance as temp_distance,c.long as lllll,c.lat as ttttt,
				ifnull(uc.collection_count,0)+ifnull(ccomment.comment_count,0) as counts')
				->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'
	        	left join s_user_distance ud on ud.center_id=c.id and ud.distance_id=$distance_id")->where($where)-> order($order) -> select();
			}else{
				$where['s_course.type'] = $_GET['id'];//app首页,体育运动1,亲子早教2,科学技术3,美术绘画4,乐器演奏5,声乐舞蹈6,国学书法7
				$courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image,ud.temp_distance as temp_distance,
				ifnull(uc.collection_count,0)+ifnull(ccomment.comment_count,0) as counts')
				->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'
	        	left join s_user_distance ud on ud.center_id=c.id and ud.distance_id=$distance_id")->where($where)-> order($order) -> select();
			}
		}else{
			if(empty($renqi)){
				$order = 'create_time desc';
			}else {
				$order = 'counts desc,create_time desc';
			}
			if(empty($_GET['id'])){
				$courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image,
				ifnull(uc.collection_count,0)+ifnull(ccomment.comment_count,0) as counts')
				->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'")->where($where)-> order($order) -> select();
			}else{
				$where['s_course.type'] = $_GET['id'];//app首页,体育运动1,亲子早教2,科学技术3,美术绘画4,乐器演奏5,声乐舞蹈6,国学书法7
				$courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image,
				ifnull(uc.collection_count,0)+ifnull(ccomment.comment_count,0) as counts')
				->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'")->where($where)-> order($order) -> select();
			}
		}
		$counts = $course_image ->count();
		$user_distance=M('user_distance');
		$options['distance_id']=$distance_id;
		$user_distance->where($options)->delete();

		//        echo var_dump($courseList);
		//        $this->display();
		$result['courseList']=$courseList;
		$result['counts']=$counts;
		$result['sear']=$_GET['search'];
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 课程详细
	 * 入参,课程id
	 * get
	 */
	public function courseDitail(){
		$user_click = M('Course');
		$where['id'] = $_GET['id'];//$_GET['id']
		$count=$user_click->where($where)->getField('number');
		//查看列表查看次数加一
		$counts = (int)$count + 1;
		$data['number'] = $counts;
		$user_click->where($where)->data($data)->save();
		//课程
		$course = M('Course');
		$courseList = $course->where($where)->find();
		//二级课程
		$two_course = M('Two_course');
		$course_id['course_id'] = $_GET['id'];
		$twoCourseList = $two_course->where($course_id)->select();
		//课程图片
		$course_image = M('Course_image');
		$courseImageList = $course_image->where($course_id)->select();
		//评论
		$course_comment = M('Course_comment');
		$courseCommentList = $course_comment->field('s_course_comment.*,u.username,u.image as image_user')->join('left join s_user u on u.id=s_course_comment.user_id')->where($course_id)->order('s_course_comment.create_time desc')->select();
		$counts=$course_comment->where($course_id)->count();
		//机构,学校
		$center = M('Center');
		$course_id_c['c.id'] = $_GET['id'];
		$centerList = $center->field('s_center.*')->join('right join s_course c on c.center_id=s_center.id')-> where($course_id_c)->find();
		 
		$result['course'] = $courseList;//对象课程
		$result['counts'] = $counts;//对象课程
		$result['twoCourseList'] = $twoCourseList;//列表二级课程
		$result['courseImageList'] = $courseImageList;//列表课程图片
		$result['courseCommentList'] = $courseCommentList;//列表评论
		$result['centerList'] = $centerList;//对象机构,学校
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 课程评论列表
	 * 入参,课程id
	 * get
	 */
	public function courseCommentList(){
		$where['course_id'] = $_GET['id'];//$_GET['id']
		$course_comment = M('Course_comment');
		$courseCommentList=$course_comment->where($where)->select();
		$where_image['course_comment_id'] = $_GET['id'];//$_GET['id']
		$course_comment_image = M('Course_comment_image');
		$courseCommentImage=$course_comment_image->where($where_image)->select();
		$result['courseCommentList'] = $courseCommentList;//对象机构,学校
		$result['courseCommentImage'] = $courseCommentImage;//对象机构,学校
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 评论课程
	 * 入参,课程id,用户id(session获取),评论内容message
	 * post
	 */
	public function commentCourse(){
		$course_comment = M('Course_comment');
		$course_comment->create();
		$data['course_id'] = $_POST['course_id'];//$_GET['id']
		$data['user_id'] = $_POST['user_id'];//$_GET['id']
		$data['create_time'] = date('Y-m-d H:i',time());//$_GET['id']
		$data['message'] = $_POST['message'];//$_GET['id']
//		$courseComment=$course_comment->data($data)->add();
		
		$suc = $course_comment->add($data);
		$sss = '';
		$yyyy = '';
		//base64Data
		$base64=$_POST['base64Data'];
		if(empty($base64) || $base64 == null || $base64 == ""){
			$n['image']=$yyyy;
		}else{
			$arr_str=explode("---",$base64);//以,为拆分条件
			for($i=1;$i<count($arr_str);$i++){
				$s = base64_decode($arr_str[$i]);
				$uniqid = time();
				$image = file_put_contents('./Public/Uploads/image/courseComment/'.$uniqid.$i.'.png', $s);
				$sss = $sss.'-'.$uniqid.$i.'.png';
				//页面用
				if($i>0){
					$notes_image['course_comment_id'] = $suc;
					$notes_image['image'] = $uniqid.$i.'.png';
					$note_image = M('Course_comment_image');
					$note_image->create();
					$note_image->data($notes_image)->add();
				}
			}
			$n['image'] = substr($sss,1);
			/*$arr_str=explode(",",$base64);//以,为拆分条件
			$s = base64_decode($arr_str[1]);
	//		$s = base64_decode(str_replace('data:image/jpeg;base64,', '', $base64));
			$uniqid = 'note'.time();
			$image = file_put_contents('./Public/Uploads/image/noteTwo/'.$uniqid.'.png', $s);
			$noteTwo['image'] = $uniqid.'.png';*/
		}
		$where['id'] = $suc;
		if($n['image'] == false){
			$n['image'] = '';
		}
		$course_comment->data($n)->where($where)->save();
		
		$result['code'] = '1';
		$result['msg'] = '评论成功';
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 申请体验表
	 * 入参,学生姓名student_name,学生性别student_sex,学生生日date,家长姓名parent_name,家长性别parent_sex,家长电话phone
	 * post
	 */
	public function courseTypesq(){
		if(empty($_SESSION['user'])){
			$result['code']='0';
			$this->ajaxReturn($result,'JSON');
		}else{
			$course_type = M('Course_type');
			$courseType = $course_type ->order('id asc')-> select();
			$result['code']='1';
			$result['courseType'] = $courseType;
			$this->ajaxReturn($result,'JSON');
		}
	}
	/**
	 * 分类表
	 */
	public function courseType(){
		$course_type = M('Course_type');
		$courseType = $course_type ->order('id asc')-> select();
		$result['code']='1';
		$result['courseType'] = $courseType;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 全部评论
	 */
	public function commentAll(){
		$course_comment = M('Course_comment');
		$where['course_id'] = $_GET['id'];
		$courseComment = $course_comment->field('s_course_comment.*,u.username,u.image as image_user')->join('left join s_user u on u.id = s_course_comment.user_id') ->where($where)->order('create_time desc')-> select();
		$course_comment_image = M('Course_comment_image');
		$where['course_id'] = $_GET['id'];
		$courseCommentImage=$course_comment_image->where($where_image)->select();
		$result['courseCommentImage'] = $courseCommentImage;//对象机构,学校
		$result['code']='1';
		$result['courseComment'] = $courseComment;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 我的收藏
	 */
	public function collectionList(){
		$user_collection = M('User_collection');
		$where['s_user_collection.user_id'] = $_GET['id'];
		$userCollection = $user_collection->field("s_user_collection.*,c.course_name,ci.image,sc.name as center_name,c.type as course_type")
		->join("left join s_course c on c.id = s_user_collection.course_id
    		left join s_center sc on c.center_id = sc.id 
        	left JOIN s_course_image ci on c.id = ci.course_id and ci.type='1'")
		->where($where) ->order('s_user_collection.create_time desc')-> select();
		$result['code']='1';
		$result['userCollection'] = $userCollection;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 *收藏
	 */
	public function addCollection(){
		$user_collection = M('User_collection');
		$user_collection->create();
		$curr_time = date('Y-m-d H:i',time());
		$user_collection->create_time=$curr_time;
		$user_collection->user_id= $_GET['user_id'];
		$user_collection->course_id=$_GET['course_id'];
		$id = $user_collection->add();
		if(empty($id)){
			$result['code']='1';
			$result['msg']='收藏失败';
			$this->ajaxReturn($result,'JSON');
		}else{
			$result['code']='2';
			$result['msg']='收藏成功';
			$this->ajaxReturn($result,'JSON');
		}
	}

	/**
	 * 取消收藏
	 */
	public function delCollection(){
		$game = M("User_collection");
		//根据用户和课程取消
		$where['user_id'] = $_GET['user_id'];
		$where['course_id'] = $_GET['course_id'];
		//$where['id']=$_POST['id'];
		$id = $game->where($where)->delete(); // 删除
		$result['code']='取消收藏成功';
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 是否收藏
	 */
	public function sfCollection(){
		$game = M("User_collection");
		//根据用户和课程取消
		$where['user_id'] = $_GET['user_id'];
		$where['course_id'] = $_GET['course_id'];
		//$where['id']=$_POST['id'];
		$id = $game->where($where)->select();
		if(empty($id)){
			$result['code']='1';
			$result['msg']='未收藏';
			$this->ajaxReturn($result,'JSON');
		}else{
			$result['code']='2';
			$result['msg']='已收藏';
			$this->ajaxReturn($result,'JSON');
		}
	}
	/**
	 * 提交申请
	 */
	public function addApplication(){
		$data['course_id'] = $_POST['course_id'];
		$data['user_id'] = $_POST['user_id'];
		$data['student_name'] = $_POST['student_name'];
		$data['student_sex'] = $_POST['student_sex'];
		$data['brithday'] = $_POST['brithday'];
		$data['parent_name'] = $_POST['parent_name'];
		$data['parent_sex'] = $_POST['parent_sex'];
		$data['phone'] = $_POST['phone'];
		$data['order_status'] = '已预约';
		$user_appointment = M('User_appointment');
		$user_appointment->create();
		$curr_time = date('Y-m-d H:i',time());
		$data['create_time'] = $curr_time;
		//$user_appointment->create_time=$curr_time;
		$id = $user_appointment->data($data)->add();
		$result['code'] = '您的申请已提交，我们的客服会在2个工作日内与您取得联系，请确保手机畅通！';
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 我的预约
	 */
	public function myApplication(){
		$user_appointment = M('User_appointment');
		$where['s_user_appointment.user_id'] = $_GET['id'];
		$userAppointment = $user_appointment->field("s_user_appointment.*,c.course_name,ci.image,sc.name as center_name,c.type as course_type")
		->join("left join s_course c on c.id = s_user_appointment.course_id
    		left join s_center sc on c.center_id = sc.id 
        	left JOIN s_course_image ci on c.id = ci.course_id and ci.type='1'")
		->where($where) ->order('s_user_appointment.create_time desc')-> select();
		$result['code']='1';
		$result['userAppointment'] = $userAppointment;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 我的预约详细
	 */
	public function myApplicationDetail(){
		$user_appointment = M('User_appointment');
		$where['s_user_appointment.id'] = $_GET['id'];
		$userAppointment = $user_appointment->field('s_user_appointment.*,c.course_name,sc.name as center_name,
    	(year(now()) - year(s_user_appointment.brithday)) as years')
		->join('left join s_course c on c.id = s_user_appointment.course_id
    	left join s_center sc on c.center_id = sc.id ')->where($where)->find();
		$result['code']='1';
		$result['userAppointment'] = $userAppointment;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 全部评论
	 */
	public function courseName(){
		$course_comment = M('Course');
		$where['id'] = $_GET['id'];
		$course_name = $course_comment->where($where)->getField('course_name');;
		//$result['courseComment'] = $courseComment;
		$this->ajaxReturn($course_name,'JSON');
	}
	/**
	 * 计算经纬度用
	 * Enter description here ...
	 * @param unknown_type $user_long
	 * @param unknown_type $user_lat
	 * @param unknown_type $city
	 */
	private  function updateUserDistance($user_long,$user_lat)
	{
			
		$course=M('Center');
		$members=$course->field('s_center.*')->join('RIGHT JOIN s_course sc on sc.center_id = s_center.id')->select();
		$options['distance_id']=rand(1,9999);
		$distance_id=$options['distance_id'];
		foreach ($members as $key=>$value)
		{
			$user_distance=M('User_distance');
			$temp_distance=round((float)getdistance($user_long,$user_lat,$value['long'],$value['lat'])/1000,2);
			$options['temp_distance']=$temp_distance;
			$options['center_id']=$value['id'];
			//$options['id']=UUIDUtil::getOneUUID();//主键设置为UUID(varchar类型)
			$user_distance->data($options)->add();
		}
		return $distance_id;
	}
	/**
	 *求两个已知经纬度之间的距离,单位为米
	 *@param lng1,lng2 经度
	 *@param lat1,lat2 纬度
	 *@return float 距离，单位米
	 *@author www.Alixixi.com
	 **/
	/*function getdistance($lng1,$lat1,$lng2,$lat2){
		//将角度转为狐度
		$radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度
		$radLat2=deg2rad($lat2);
		$radLng1=deg2rad($lng1);
		$radLng2=deg2rad($lng2);
		$a=$radLat1-$radLat2;
		$b=$radLng1-$radLng2;
		$s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;
		return $s;
		}*/
	/**
	 * 二级课程列表
	 */
	public function courseTwoList(){
		$where['type'] = $_GET['id'];//$_GET['id']
		$course_two = M('Course_two');
		$courseTwoList=$course_two->where($where)->select();
		$result['courseTwoList'] = $courseTwoList;
		$this->ajaxReturn($result,'JSON');
	}
}
?>